require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')
require('../libs/iconfont/iconfont.css')
require('../libs/swiper/swiper-bundle.min.css')

require('../css/home.less')
require('weui')

const Swiper = require('../libs/swiper/swiper-bundle.min')

const utilButtomNav = require('../util/util')

const axios = require('axios')

const weui = require('weui.js')


document.addEventListener('DOMContentLoaded', function () {
    const rank = document.querySelector('.rank')
    const serUrl = 'http://139.9.177.51:8099'
    const homeDataPathName = '/headPageInfo'
    const clockPathName = '/clockIn'
    const userId = localStorage.getItem('userId')
    console.log(userId);
    //引入组件
    utilButtomNav.ButtomNav('home')

    //初始化swiper插件
    let mySwiper = new Swiper('.swiper', {
        loop: true, // 循环模式选项
        autoplay: true,
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },
    })
    //ajax渲染首页数据
    axios.get(serUrl + homeDataPathName,
        {
            params: {
                userId: userId
            }
        }).then(function (res) {
            console.log(res);
            // 排名
            if (res.data.data.rank > 10) {
                rank.style.fontSize = '16px'
                rank.textContent = res.data.data.rank
            } else {
                rank.textContent = res.data.data.rank
            }
            // 徽章数量
            if (res.data.data.insigniaNum > 10) {
                raninsigniaNumk.style.fontSize = '16px'
                insigniaNum.textContent = res.data.data.insigniaNum
            } else {
                insigniaNum.textContent = res.data.data.insigniaNum
            }
            // 打卡天数
            punchIn.textContent = res.data.data.punchIn

            // 是否打卡
            if (res.data.data.isPunch === 'false') {
                isPunch.textContent = '今日打卡'
            } else {
                isPunch.textContent = '已打卡'
            }
        })

    //打卡
    isPunch.addEventListener('click', function () {
        axios.get(serUrl + clockPathName, {
            params: {
                userId: userId
            }
        }).then(function (res) {
            console.log(res);
            if (res.data.status === 0) {
                weui.toast('打卡成功') 
                punchIn.textContent = parseInt(punchIn.textContent) + 1
                isPunch.textContent = '已打卡'
            } else {
                weui.toast(res.data.msg)    
            }
        })

    })
    //点击运动数据跳转至详情页
    document.querySelector("#sportData").addEventListener('click',function(){
        location.href = './sportsData.html'
    })
})       
